package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.monitoring.j_monitoring.constantConfig.MonitorConstant;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlInformationSystem;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlVirtualMachine;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlVirtualizationCluster;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlInformationSystemMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlInformationSystemService;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlVirtualMachineService;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlVirtualizationClusterService;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 信息系统(管理)
 * @Author: xiaoyq
 * @Date:   2023-02-23
 * @Version: V1.0
 */
@Service
public class MGlInformationSystemServiceImpl extends ServiceImpl<MGlInformationSystemMapper, MGlInformationSystem> implements IMGlInformationSystemService {
    @Override
    public String getNewNo(){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");

        int todayCount = baseMapper.getTodayCount();
        todayCount+=1;
        if (todayCount<=9 ){
            return sdf.format(System.currentTimeMillis()) + "00" + todayCount;
        }


        if (todayCount>9 && todayCount<=99){
            return sdf.format(System.currentTimeMillis()) + "0" + todayCount;
        }


        return sdf.format(System.currentTimeMillis()) + todayCount;
    }

    @Override
    public JSONObject getDeviceMapById(JSONArray nodes, JSONArray edges, int startX, int startY, String id) {
        JSONObject result;
        MGlInformationSystem entity = this.getById(id);
        JSONObject child,edge;
        child = new JSONObject();
        child.put("category", MonitorConstant.Application);
        child.put("id","_APP_ID_"+entity.getSystemName());
        child.put("name",entity.getSystemName());
        child.put("x",startX + MonitorConstant.StepX * 0);
        child.put("y",startY);

        result = child;

        nodes.add(child);

        IMGlVirtualMachineService virtualMachineService = ApplicationContextUtil.getContext().getBean(IMGlVirtualMachineService.class);
        IMGlVirtualizationClusterService clusterService = ApplicationContextUtil.getContext().getBean(IMGlVirtualizationClusterService.class);
        QueryWrapper<MGlVirtualMachine> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MGlVirtualMachine::getAssociatedApplication,entity.getSystemName());
        List<MGlVirtualMachine> machineList = virtualMachineService.list(queryWrapper);
        MGlVirtualizationCluster cluster;
        int i = 0;
        Map<String,MGlVirtualizationCluster> clusterMap = new HashMap<>();
        for (MGlVirtualMachine machine : machineList){
            startY = startY + MonitorConstant.StepY * i;
            child = new JSONObject();
            child.put("category", MonitorConstant.VirtualMachine);
            child.put("id",machine.getId());
            child.put("ip",machine.getVirtualIp());
            child.put("name",machine.getName());
            child.put("x",startX + MonitorConstant.StepX * 1);
            child.put("y",startY );

            nodes.add(child);

            edge = new JSONObject();
            edge.put("source","_APP_ID_"+entity.getSystemName());
            edge.put("target",machine.getId());
            edges.add(edge);

            if(StringUtils.isNotBlank(machine.getClusterId())){
                if(clusterMap.containsKey(machine.getClusterId())){
                    cluster = clusterMap.get(machine.getClusterId());
                }
                else{
                    cluster = clusterService.getById(machine.getClusterId());

                    child = new JSONObject();
                    child.put("category", MonitorConstant.VirtualCluster);
                    child.put("id",cluster.getId());
                    child.put("ip",cluster.getIpAddress());
                    child.put("name",cluster.getName());
                    child.put("x",startX + MonitorConstant.StepX * 2);
                    child.put("y",startY );

                    nodes.add(child);

                    clusterMap.put(cluster.getId(),cluster);
                }
//                JSONObject root = clusterService.getDeviceMapById(nodes,edges,startX + MonitorConstant.StepX * 2,startY,machine.getClusterId());
                edge = new JSONObject();
                edge.put("source",machine.getId());
                edge.put("target",machine.getClusterId());
                edges.add(edge);
            }

            i += 1;
        }

        return result;
    }
}
